//
//  ViewController.m
//  ReactiveCocoa
//
//  Created by apple on 2017/8/1.
//  Copyright © 2017年 apple. All rights reserved.
//

#import "ViewController.h"
#import <ReactiveObjC/ReactiveObjC.h>
#import "NSObject+RACKVOWrapper.h"
#import "RACView.h"
@interface ViewController ()

@property (nonatomic, strong) RACView *racView;

@property (nonatomic, strong) NSArray *lArr;

@property (nonatomic, strong) UILabel *label;

@property (nonatomic, copy) NSString *textFieldStr;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(100, 500, 100, 40)];
    
    label.text = @"我";
    
    label.textColor = [UIColor redColor];
    
    label.backgroundColor = [UIColor greenColor];
    
    [self.view addSubview:label];
    
    self.label = label;
    
    
    //RACSignal - 创建冷信号
    RACSignal *signal = [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
        
        //发送信号
        [subscriber sendNext:@"我发送啦"];
        
        return nil;
    }];
    
    //订阅信号 - 变为热信号
    [signal subscribeNext:^(id  _Nullable x) {
        
        NSLog(@"signal - x = %@",x);
        
    }];
    
    
    
    //RAC 解析字典 数组同理
    NSDictionary *dic = @{@"name" : @"烯烯", @"sex" : @"man", @"age" : @18};
    
    [dic.rac_sequence.signal subscribeNext:^(RACTuple * x) {
        
        RACTupleUnpack(NSString *s, NSString *y) = x;
        
        NSLog(@"s = %@, y = %@",s, y);
    }];
    
    [dic.rac_valueSequence.signal subscribeNext:^(id  _Nullable x) {
        
        NSLog(@"x = %@", x);
        self.textFieldStr = x;
    }];
    
    
    
    self.racView = [[RACView alloc] init];
    
    self.racView.frame = CGRectMake(50, 200, 200, 200);
    
    [self.view addSubview:self.racView];
    
    
    //代替代理传值 - RACSubject - 可以传值 - 下面有个不能传值的代替代理的方法
    //订阅信号 变成热信号
    [self.racView.viewSignal subscribeNext:^(id  _Nullable x) {
       
        self.lArr = x;
        NSLog(@"x = %@",x);
        
        self.label.text = self.lArr.lastObject;
    }];
    

    
    //代替btn点击事件 - 监听
    [self createBTNRAC];
    //代替文本框 - 监听文本框输入
    [self createTextFieldRAC];
    
    
    //代替代理 - but 不能传值 - 传值的话还是用RACSubject
    [[self.racView rac_signalForSelector:@selector(Click:)] subscribeNext:^(RACTuple * _Nullable x) {
       
        NSLog(@"此方法代替代理但是不能传值，RACView上的btn被点击了");
    }];
    
    
    //代替通知 - RAC - 跟代替btn监听一样 - 把实现方法都放在了一个代码块中 - 高聚合
    [[[NSNotificationCenter defaultCenter] rac_addObserverForName:@"RACView" object:nil] subscribeNext:^(NSNotification * _Nullable x) {
        
        //这里写接收到通知要做的事情 - 不用单独在出去写个方法了 - 高聚合
        NSLog(@"NSNotificationCenter - X = %@",x.userInfo[@"RACView"]);
        
        self.view.backgroundColor = [UIColor orangeColor];
        
    }];
    
    
    //代替KVO - RAC - 把实现方法都放在了一个代码块中 - 高聚合
    [self.racView rac_observeKeyPath:@"btn.frame" options:NSKeyValueObservingOptionNew observer:nil block:^(id value, NSDictionary *change, BOOL causedByDealloc, BOOL affectedOnlyLastComponent) {
       
//        NSLog(@"代替KVO - RAC -btn.frame -改变了 -change = %@",change);
//        NSLog(@"代替KVO - RAC -btn.frame -改变了 -value = %@",value);
//        NSLog(@"代替KVO - RAC -btn.frame -改变了 -causedByDealloc = %d",causedByDealloc);
//        NSLog(@"代替KVO - RAC -btn.frame -改变了 -affectedOnlyLastComponent = %d",affectedOnlyLastComponent);
        
    }];
    
    //代替KVO - RAC - 把实现方法都放在了一个代码块中 - 高聚合 - 和上面方法的区别是 - 这个不用导入头文件#import "NSObject+RACKVOWrapper.h"
    //这个和其它的一样 是返回一个冷信号 - 而上面的方法不是
    [[self.racView rac_valuesForKeyPath:@"btn.frame" observer:self] subscribeNext:^(id  _Nullable x) {
       
        NSLog(@"代替KVO - RAC -btn.frame -改变了 -change = %@",x);
    }];
    
    
    
    
    
}







//RAC 监听btn点击事件 - 代替addtarget
- (void)createBTNRAC{

    //监听btn点击事件
    UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0 , 100, 50)];
    
    [btn setTitle:@"点击" forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor greenColor] forState:UIControlStateNormal];
    
    btn.backgroundColor = [UIColor redColor];
    
    [self.view addSubview:btn];
    
    [[btn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
        
        NSLog(@"btn = %@", x);
        
        //这个X就是btn - 2333
        x.backgroundColor = [UIColor grayColor];
        //        btn.backgroundColor = [UIColor grayColor];
    }];
}


//监听文本框改变- RAC
- (void)createTextFieldRAC{

    UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(200, 0 , 100, 50)];
    
    textField.backgroundColor = [UIColor blueColor];
    
    [self.view addSubview:textField];
    
    //这个相当于给textField addtarget action  - 监听输入数值的改变
    [textField.rac_textSignal subscribeNext:^(NSString * _Nullable x) {
       
        //输入一个数，便打印出来
        NSLog(@"textField - x = %@",x);
        
        self.textFieldStr = x;
    }];
}


- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{

    NSLog(@"textField Str = %@", self.textFieldStr);
    
    self.racView.btn.frame = CGRectMake(10, 30, 40, 40);
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)dealloc{

    
}

@end
